VERSION 5.00
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
Object = "{3B7C8863-D78F-101B-B9B5-04021C009402}#1.2#0"; "RICHTX32.OCX"
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
Begin VB.Form frmMain 
   Caption         =   "Nightowl"
   ClientHeight    =   5745
   ClientLeft      =   225
   ClientTop       =   855
   ClientWidth     =   8730
   BeginProperty Font 
      Name            =   "Tahoma"
      Size            =   12
      Charset         =   0
      Weight          =   400
      Underline       =   0   'False
      Italic          =   0   'False
      Strikethrough   =   0   'False
   EndProperty
   Icon            =   "frmMain.frx":0000
   KeyPreview      =   -1  'True
   LinkTopic       =   "Form1"
   ScaleHeight     =   5745
   ScaleWidth      =   8730
   StartUpPosition =   3  'Windows Default
   Begin RichTextLib.RichTextBox txtInput 
      Height          =   615
      Left            =   120
      TabIndex        =   8
      Top             =   4800
      Width           =   6135
      _ExtentX        =   10821
      _ExtentY        =   1085
      _Version        =   393217
      Enabled         =   -1  'True
      MultiLine       =   0   'False
      TextRTF         =   $"frmMain.frx":37A2
   End
   Begin VB.Timer BufferFlush 
      Interval        =   200
      Left            =   6000
      Top             =   0
   End
   Begin VB.Timer FloodProtector 
      Interval        =   30000
      Left            =   5520
      Top             =   0
   End
   Begin MSComctlLib.StatusBar sBar 
      Align           =   2  'Align Bottom
      Height          =   255
      Left            =   0
      TabIndex        =   7
      Top             =   5490
      Width           =   8730
      _ExtentX        =   15399
      _ExtentY        =   450
      _Version        =   393216
      BeginProperty Panels {8E3867A5-8586-11D1-B16A-00C0F0283628} 
         NumPanels       =   4
         BeginProperty Panel1 {8E3867AB-8586-11D1-B16A-00C0F0283628} 
            Text            =   "Connected!"
            TextSave        =   "Connected!"
         EndProperty
         BeginProperty Panel2 {8E3867AB-8586-11D1-B16A-00C0F0283628} 
            AutoSize        =   1
            Object.Width           =   6604
            MinWidth        =   6075
            Text            =   "#longest-possible-channel-name"
            TextSave        =   "#longest-possible-channel-name"
         EndProperty
         BeginProperty Panel3 {8E3867AB-8586-11D1-B16A-00C0F0283628} 
            Object.Width           =   2469
            MinWidth        =   2469
            Text            =   "100 members"
            TextSave        =   "100 members"
         EndProperty
         BeginProperty Panel4 {8E3867AB-8586-11D1-B16A-00C0F0283628} 
            Object.Width           =   3175
            MinWidth        =   3175
            Text            =   "Global Ignore Enabled"
            TextSave        =   "Global Ignore Enabled"
         EndProperty
      EndProperty
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "Tahoma"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
   End
   Begin VB.ListBox lstIgnore 
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   238
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   270
      Left            =   2280
      TabIndex        =   6
      Top             =   120
      Visible         =   0   'False
      Width           =   2175
   End
   Begin RichTextLib.RichTextBox txtHistory 
      CausesValidation=   0   'False
      Height          =   3975
      Left            =   120
      TabIndex        =   3
      Top             =   720
      Width           =   6135
      _ExtentX        =   10821
      _ExtentY        =   7011
      _Version        =   393217
      BorderStyle     =   0
      ReadOnly        =   -1  'True
      ScrollBars      =   2
      OLEDragMode     =   0
      OLEDropMode     =   0
      TextRTF         =   $"frmMain.frx":381D
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   238
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
   End
   Begin MSComDlg.CommonDialog cdg1 
      Left            =   4560
      Top             =   0
      _ExtentX        =   847
      _ExtentY        =   847
      _Version        =   393216
   End
   Begin VB.ListBox lstUsers 
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   8.25
         Charset         =   238
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   4470
      Left            =   6360
      Sorted          =   -1  'True
      TabIndex        =   1
      Top             =   720
      Width           =   2295
   End
   Begin VB.Label Label3 
      Alignment       =   1  'Right Justify
      BeginProperty Font 
         Name            =   "Tahoma"
         Size            =   9.75
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   4920
      TabIndex        =   5
      Top             =   120
      Width           =   3495
   End
   Begin VB.Label lbltitle 
      BeginProperty Font 
         Name            =   "Tahoma"
         Size            =   9.75
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   480
      TabIndex        =   4
      Top             =   120
      Width           =   2895
   End
   Begin VB.Shape shpSquare 
      BorderStyle     =   0  'Transparent
      FillColor       =   &H00FF0000&
      FillStyle       =   0  'Solid
      Height          =   255
      Left            =   120
      Top             =   120
      Width           =   255
   End
   Begin VB.Label Label2 
      BeginProperty Font 
         Name            =   "Tahoma"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   6360
      TabIndex        =   0
      Top             =   480
      Width           =   1455
   End
   Begin VB.Label Label1 
      BeginProperty Font 
         Name            =   "Tahoma"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   120
      TabIndex        =   2
      Top             =   480
      Width           =   2295
   End
   Begin VB.Menu mnu1File 
      Caption         =   ""
      Begin VB.Menu mnuClearHistory 
         Caption         =   ""
      End
      Begin VB.Menu mnuSave 
         Caption         =   ""
         Shortcut        =   ^S
      End
      Begin VB.Menu mnuFS1 
         Caption         =   "-"
      End
      Begin VB.Menu mnuExit 
         Caption         =   ""
      End
   End
   Begin VB.Menu MNU1Server 
      Caption         =   ""
      Begin VB.Menu MNUServList 
         Caption         =   ""
         Shortcut        =   ^F
      End
      Begin VB.Menu MNUDisconnect 
         Caption         =   ""
         Shortcut        =   ^Q
      End
   End
   Begin VB.Menu mnu1Channel 
      Caption         =   ""
      Begin VB.Menu mnuList 
         Caption         =   ""
         Shortcut        =   ^G
      End
      Begin VB.Menu mnuJoin 
         Caption         =   ""
         Shortcut        =   ^J
      End
      Begin VB.Menu mnuPart 
         Caption         =   ""
         Enabled         =   0   'False
         Shortcut        =   ^L
      End
      Begin VB.Menu mnu2S1 
         Caption         =   "-"
      End
      Begin VB.Menu mnuGetBan 
         Caption         =   ""
         Shortcut        =   {F7}
      End
      Begin VB.Menu mnuSTopic 
         Caption         =   ""
      End
      Begin VB.Menu mnu2S2 
         Caption         =   "-"
      End
   End
   Begin VB.Menu MNU1User 
      Caption         =   ""
      Begin VB.Menu mnuCHNick 
         Caption         =   ""
         Shortcut        =   ^N
      End
      Begin VB.Menu mnuWhois 
         Caption         =   ""
      End
      Begin VB.Menu mnuGIgnore 
         Caption         =   ""
         Shortcut        =   +{F12}
      End
      Begin VB.Menu mnuClearIgnore 
         Caption         =   ""
         Shortcut        =   ^{F12}
      End
      Begin VB.Menu mnuZTI 
         Caption         =   ""
         Shortcut        =   +^{F12}
      End
   End
   Begin VB.Menu mnu1Options 
      Caption         =   ""
      Begin VB.Menu mnuSynth 
         Caption         =   ""
      End
      Begin VB.Menu mnuSAPI 
         Caption         =   ""
         Enabled         =   0   'False
      End
      Begin VB.Menu mnu4S2 
         Caption         =   "-"
      End
      Begin VB.Menu mnuSound 
         Caption         =   ""
      End
      Begin VB.Menu mnuAutoLog 
         Caption         =   ""
      End
      Begin VB.Menu mnuCExit 
         Caption         =   ""
      End
      Begin VB.Menu mnu4S3 
         Caption         =   "-"
      End
      Begin VB.Menu mnuLargeSub 
         Caption         =   ""
         Begin VB.Menu mnuLarge 
            Caption         =   ""
            Checked         =   -1  'True
            Index           =   0
         End
         Begin VB.Menu mnuLarge 
            Caption         =   ""
            Index           =   1
         End
         Begin VB.Menu mnuLarge 
            Caption         =   ""
            Index           =   2
         End
         Begin VB.Menu mnuLarge 
            Caption         =   ""
            Index           =   3
         End
      End
      Begin VB.Menu mnu4S4 
         Caption         =   "-"
      End
      Begin VB.Menu mnuSWatch 
         Caption         =   ""
         Shortcut        =   ^W
      End
      Begin VB.Menu mnuOWatch 
         Caption         =   ""
      End
   End
   Begin VB.Menu mnu1Help 
      Caption         =   ""
      Begin VB.Menu mnuHelp 
         Caption         =   ""
         Shortcut        =   {F1}
      End
      Begin VB.Menu MNUFb 
         Caption         =   ""
      End
      Begin VB.Menu mnu5S1 
         Caption         =   "-"
      End
      Begin VB.Menu mnuWeb 
         Caption         =   ""
      End
      Begin VB.Menu mnuMOTD 
         Caption         =   ""
      End
      Begin VB.Menu mnuAbout 
         Caption         =   ""
         Shortcut        =   ^{F1}
      End
   End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit


' This file is part of Nightowl.
'
' Nightowl is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.

' Nightowl is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.

' You should have received a copy of the GNU General Public License
' along with Nightowl.  If not, see <http://www.gnu.org/licenses/>.


Public WithEvents IRCInterface As clsModIRC
Attribute IRCInterface.VB_VarHelpID = -1
Public SpeechInterface As Speech
Attribute SpeechInterface.VB_VarHelpID = -1

Dim FloodFlag As Boolean
Dim SndFloodCounter As Integer

Dim FloodBuf As String

Dim onFC As Boolean
Public watchesProc As Integer

Dim ircServs As clsServices
Dim UsingFML As Boolean

Dim HostBuffer As String

Dim pcNick As String

Dim theTopic As String

Public lblMembers As Object
Public lblCurrentChan As Object
Public lblIgnore As Object
Public InviteChan As String
Public BufferTimer As Long
Public bufferIndex As Byte
Public c As Byte
Public whichvoice As Integer
Public selpos As Long
Public cfm As Byte
Private HistoryBuffer(9) As String
Private InputBuffer(9) As String
Private InpIndex As Long 'keeps track of the index of the inputBuffer







Private Sub BufferFlush_Timer()

' Don't run if not necessary.
If FloodBuf = "" Then Exit Sub

' Save our current insertion point position.
selpos = txtHistory.SelStart

' Add the text to the history box itself
txtHistory.Text = txtHistory.Text & FloodBuf

' With an error trap, attempt to reset the
' insertion point if necessary.
'On Error Resume Next
'If ActiveControl.Name <> "txtHistory" Then txtHistory.SelStart = Len(txtHistory.Text) Else txtHistory.SelStart = selpos
'On Error GoTo 0

FloodBuf = ""

End Sub

Private Sub SendInputBuffer()
  Dim wnick, Text As String
  
If modAC.GlobalIgnore = True Then
  SpeechInterface.Say lang.SpeechMsg(3), True
  txtInput.Text = ""
  txtInput.SetFocus
  Exit Sub
End If

Select Case ActiveControl.Name
Case "txtInput"
  If txtInput.Text = "" Then modSound.Ding: Exit Sub
  Select Case LCase(GetWord(txtInput.Text, 1))
  Case "/accept"
    If InviteChan = "" Then AddToHistory "There is currently no pending invitation.": Exit Sub
    AddToHistory "Accepting invitation to join " & InviteChan
    modAC.currentChannel = InviteChan
    InviteChan = ""
  Case "/msg"
    'Fix: check if there is actually an action before sending! (ace)
    If IsWord(txtInput.Text, 3) = False Then beep: txtInput.Text = "": txtInput.SetFocus: Exit Sub
    If Settings.UseSounds = 1 Then modSound.PlaySound App.Path + "\sounds\SEND.WAV", False
    IRCInterface.SendMessage GetWord(txtInput.Text, 2), GetFromWord(txtInput.Text, 3)
    AddToHistory "private to " + GetWord(txtInput.Text, 2) + " " + _
      IRCInterface.Nick + " says " + GetFromWord(txtInput.Text, 3), True
  Case "/notice"
    'Fix: check if there is actually an action before sending! (ace)
    If IsWord(txtInput.Text, 3) = False Then beep: txtInput.Text = "": txtInput.SetFocus: Exit Sub
    If Settings.UseSounds = 1 Then modSound.PlaySound App.Path + "\sounds\SEND.WAV", False
    IRCInterface.SendNotice GetWord(txtInput.Text, 2), GetFromWord(txtInput.Text, 3)
    AddToHistory "notice to " + GetWord(txtInput.Text, 2) + ": " + GetFromWord(txtInput.Text, 3), True
  Case "/whois"
    wnick = GetWord(txtInput.Text, 2)
    If wnick <> "" Then frmWhois.DoWhois wnick
  Case "/me"
    If IsWord(txtInput.Text, 2) = False Then modSound.Ding: txtInput.Text = "": txtInput.SetFocus: Exit Sub
    If InPM <> "" Then
      IRCInterface.SendCTCP InPM, "ACTION", GetFromWord(txtInput.Text, 2)
    AddToHistory "private to " + InPM + " " + _
      IRCInterface.Nick + " " + GetFromWord(txtInput.Text, 2), True
    Else
      IRCInterface.SendCTCP currentChannel, "ACTION", GetFromWord(txtInput.Text, 2)
      AddToHistory IRCInterface.Nick + " " + GetFromWord(txtInput.Text, 2), True
    End If
  Case "/invite"
    If IsChanop = False Then
      AddToHistory "You must be a Channel Host to use the /invite command.", True
    Else
      If IsWord(txtInput.Text, 2) Then
        IRCInterface.RawSend "INVITE " & GetWord(txtInput.Text, 2) & " " & modAC.currentChannel
        AddToHistory GetWord(txtInput.Text, 2) & " has been invited into the channel.", True
      Else
        modSound.Ding
      End If
    End If
  Case "/nick"
    If IsWord(txtInput.Text, 2) Then
      IRCInterface.RawSend "NICK " & GetWord(txtInput.Text, 2)
    Else
      modSound.Ding
    End If
  Case "/page"
    If IsWord(txtInput.Text, 3) Then
      IRCInterface.SendCTCP GetWord(txtInput.Text, 2), "PAGER", GetFromWord(txtInput.Text, 3)
    Else
      modSound.Ding
    End If
  Case "/part"
    ' leave the room
    mnuPart_Click
  Case "/kick"
    If IsChanop = False Then
      AddToHistory "Sorry, only Channel Hosts can kick users!", True
    Else
      If IsWord(Text, 2) Then
        If IsWord(Text, 3) Then
          IRCInterface.RawSend "KICK " & modAC.currentChannel & " " & GetWord(Text, 2) & " :" & GetFromWord(Text, 3)
        Else
          IRCInterface.RawSend "KICK " & modAC.currentChannel & " " & GetWord(Text, 2)
        End If
      End If
    End If
  Case "/say"
    SpeechInterface.Say GetFromWord(txtInput.Text, 2), True
  Case "/quit"
    Unload Me: Exit Sub
  Case "/squit"
  frmMain.Disconnect
  Case Else
  ' If blank, don't proceed.
  If txtInput.Text = "" Then modSound.Ding: txtInput.SetFocus: Exit Sub
  ' Fix - only PM after processing all other possible
  ' slash commands.
  If InPM <> "" Then
    IRCInterface.SendMessage InPM, txtInput.Text
    AddToHistory "private to " + InPM + " " + _
      IRCInterface.Nick + " says " + txtInput.Text, True
    txtInput.Text = ""
    'txtInput.SetFocus
    Exit Sub
  End If
    IRCInterface.SendMessage currentChannel, txtInput.Text
    AddToHistory IRCInterface.Nick + " says " + txtInput.Text, True
  End Select
  
  If currentChannel <> "" Then
    If LCase(GetWord(txtInput.Text, 1)) = "/whois" Then
      frmWhois.txtWhois.SetFocus
      txtInput.Text = ""
    Else
      txtInput.Text = ""
      txtInput.SetFocus
    End If
  End If
  
Case "lstUsers"
  If Settings.UseSounds = 1 Then modSound.PlaySound App.Path + "\sounds\SEND.WAV", False
  If txtInput.Text = "" Then modSound.Ding: txtInput.SetFocus: Exit Sub
  If LCase(GetWord(txtInput.Text, 1)) = "/me" Then
    IRCInterface.SendCTCP TrimNonAlpha(lstUsers.List(lstUsers.ListIndex)), "ACTION", GetFromWord(txtInput.Text, 2)
  Else
    IRCInterface.SendMessage TrimNonAlpha(lstUsers.List(lstUsers.ListIndex)), txtInput.Text
  End If
  AddToHistory "private to " + TrimNonAlpha(lstUsers.List(lstUsers.ListIndex)) + " " + _
    IRCInterface.Nick + " says " + txtInput.Text, True
  txtInput.Text = ""
  txtInput.SetFocus

End Select

End Sub

Private Sub FloodProtector_Timer()
FloodFlag = True
SndFloodCounter = 0
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

If KeyCode = vbKeyControl Then
SpeechInterface.StopSpeech
End If

'help
If KeyCode = vbKeyF2 And Shift = 0 Then
Select Case LCase(ActiveControl.Name)
  Case "txtinput"
    SpeechInterface.Say lang.Help(6), True
  Case "txthistory"
    SpeechInterface.Say lang.Help(7), True
  Case "lstusers"
    SpeechInterface.Say lang.Help(8), True
End Select
End If

If KeyCode = vbKeyControl Then
SpeechInterface.StopSpeech
End If
End Sub


Private Sub Form_Load()


Label2.Caption = lang.Messages(28)
Label1.Caption = lang.Messages(29)
lbltitle.Caption = SoftwareName
Label3.Caption = lang.Messages(31)

'menus
SetMenuCaptions

' Set initial titlebar
Me.Caption = SoftwareName + " version " + _
  Trim(Str(App.Major)) + "." + Trim(Str(App.Minor))
If App.Revision <> 0 Then Me.Caption = Me.Caption & _
  "." & CStr(App.Revision)
Me.Caption = Me.Caption & " " & Me.Tag

' Set the visual title line
lbltitle.Caption = SoftwareName

' Create objects
Set IRCInterface = New clsModIRC
' Assign the old label names to the new
' status bar panels
Set lblCurrentChan = sBar.Panels(2)
Set lblMembers = sBar.Panels(3)
Set lblIgnore = sBar.Panels(4)

' Blank the status bar
lblMembers = ""
lblCurrentChan = ""
lblIgnore = ""
sBar.Panels(1).Text = ""

' count the watches
watchesProc = WordCount(Settings.WList)


' Ensure all of our debugging messages
' are sent to the debug system for writing
' to disk in the event of disk debugging
' being enabled.
IRCInterface.DebugMode = True


' Set the proper speech type if speechType is not -1 (Added by Erion)
If Settings.SpeechType <> -1 Then
SpeechInterface.Engine = Settings.SpeechType
SpeechInterface.Initialize
End If
' Configure the menus based on settings
If Settings.LargePrint <> 0 Then mnuLarge_Click (Settings.LargePrint)
If Settings.UseSounds = 1 Then mnuSound.Checked = True
If Settings.AutoLog = 1 Then mnuAutoLog.Checked = True
mnuCExit.Checked = Settings.ConfirmExit

' Configure SAPI if necessary
If Settings.SpeechType <> -1 Then
If SpeechInterface.Engine = 5 Then
  mnuSAPI.Enabled = True
  SpeechInterface.m_voice.Rate = Settings.SAPIRate
  whichvoice = -1
  For c = 0 To SpeechInterface.m_voice.GetVoices().Count - 1
    If LCase(SpeechInterface.m_voice.GetVoices().Item(c).GetDescription) = LCase(Settings.SAPIVoice) Then whichvoice = c
  Next
  If whichvoice > 0 Then _
  Set SpeechInterface.m_voice.Voice = frmMain.SpeechInterface.m_voice.GetVoices().Item(whichvoice)
End If
End If

'Disable server list if the user has no nick.
If Settings.Nickname = "" Then
MNUServList.Enabled = False
End If


' Stop all speech
If Settings.SpeechType <> -1 Then SpeechInterface.StopSpeech
  
' Ensure we're not on any channel.
currentChannel = ""
InpIndex = -1


End Sub

Private Sub Form_Resize()
On Local Error GoTo noresize

' We won't go smaller than default size.
If Me.Height < 6060 Then Me.Height = 6060
If Me.Width < 8820 Then Me.Width = 8820

' Perform control resizing.
txtHistory.Width = Me.Width - 2685
txtHistory.Height = Me.Height - 2415

txtInput.Top = Me.Height - 1590
txtInput.Width = txtHistory.Width

lstUsers.Left = Me.Width - 2460
lstUsers.Height = Me.Height - 1875

Label3.Left = Me.Width - 3900
Label2.Left = lstUsers.Left

Exit Sub

noresize:
On Error GoTo 0

End Sub

Private Sub Form_Unload(Cancel As Integer)
Dim oForm As Form


' Exiting program!!
' Should we really?
If IRCInterface.Connected = True And mnuCExit.Checked = True Then
  cfm = MsgBox("Are you sure you want to exit Nightowl?", vbYesNo Or vbQuestion, "Confirm Exit")
  If cfm = vbNo Then Cancel = True: Exit Sub
End If

' Global ignore sounds fix.
' If ignore is on, we disabled sounds. Turn
' them back on in config so that the proper
' settings are saved to registry.
If frmMain.mnuSound.Checked = True Then Settings.UseSounds = 1

' If using SAPI, save current SAPI voice settings
If SpeechInterface.Engine = 5 Then
Settings.SAPIRate = SpeechInterface.m_voice.Rate
Settings.SAPIVoice = SpeechInterface.m_voice.Voice.GetDescription
End If

' Save all current settings
modAC.SaveSettings

' If connected, disconnect first.
If IRCInterface.Connected = True Then
  IRCInterface.Delink Settings.QuitMsg
End If

' Wait until the disconnect is successful.
Do Until IRCInterface.Connected = False: DoEvents: Loop

' Speak exit messages
SpeechInterface.StopSpeech
SpeechInterface.Say SPrintF(lang.SpeechMsg(4), SoftwareName), True
modSound.PlaySound App.Path + "\sounds\EXIT.WAV", False

' Murder our objects!
Set IRCInterface = Nothing
'Unload sapi or WE, added by Erion
SpeechInterface.Unload
Set SpeechInterface = Nothing

' Close files
Close

' Unload any stray forms
For Each oForm In Forms
Unload oForm
Next

' And we are done!!

End

End Sub

Sub AddToHistory(ByVal Text As String, Optional Speak As Boolean)
' If ignore's on, don't do anything
If modAC.GlobalIgnore = True Then Exit Sub

' Add the text to the history box itself
FloodBuf = FloodBuf & Text & vbCrLf

'Add the sent text to the history buffer
Call AddToBuffer(Text, HistoryBuffer)

' if auto log is on, log. With error trap.
On Error Resume Next
Print #2, Text
On Error GoTo 0

' If we should speak, then we do so.
If Speak = True Then
  SpeechInterface.Say Text, False
End If

' all done.

End Sub

Private Sub IRCInterface_ChanMsg(ByVal Text As String, ByVal Nick As String, ByVal mask As String, ByVal Dest As String)
If modAC.InPM <> "" Then Exit Sub
' check for ignore
If GetWord(GetMask(Nick), 1) = "1" Then Exit Sub

AddToHistory Nick + " says " + StripCodes(Text), True
End Sub

Private Sub IRCInterface_Connect(ByVal ServerName As String)
' Since we successfully connected, we can clear our flag.
onFC = True

End Sub

Private Sub IRCInterface_CTCP(ByVal Command As String, ByVal Params As String, ByVal Nick As String, ByVal mask As String, ByVal Dest As String)
Dim cchck As Byte

Select Case Command
  Case "VERSION"
    IRCInterface.SendCTCPReply Nick, "VERSION", frmMain.Caption
  Case "ACTION"
    If modAC.InPM <> "" Then Exit Sub
    ' check for ignore
    If GetWord(GetMask(Nick), 1) = "1" Then Exit Sub
    AddToHistory Nick + " " + Params, True
  Case "PAGER"
    ' check for global ignore
    If GlobalIgnore = True Then Exit Sub
    
    ' check for ignore
    If GetWord(GetMask(Nick), 4) = "1" Then
      IRCInterface.SendCTCPReply Nick, "PMCHATREQUEST", "DENY"
      Exit Sub
    End If
    If Settings.UseSounds = 1 Then
      SndFloodCounter = SndFloodCounter + 1
      ' 5 sounds per 30 secs
      If SndFloodCounter < 6 Then modSound.PlaySound App.Path + "\sounds\PAGE.WAV", False
    End If
    If modAC.currentChannel = "" Then
      MsgBox "Page from " + Nick + ":" + vbCrLf + Params, vbOKOnly + vbInformation, "Page"
    Else
      AddToHistory "Page from " + Nick + ": " + Params, True
    End If
  Case "LEAVINGPMCHAT"
    If InPM = "" Then Exit Sub
    If Left$(Dest, 1) = "#" Then Exit Sub
    InPM = ""
    If UsingFML = True Then Nick = Right$(Nick, Len(Nick) - 4)
    AddToHistory Nick + " has ended the private conversation.", True
  Case "PMCHATREQUEST"
    UsingFML = False
    ' check for FloodFlag
    If FloodFlag = False Then
      IRCInterface.SendCTCPReply Nick, "PMCHATREQUEST", "DENY FLOOD"
      Exit Sub
    End If
    FloodFlag = False
    If modAC.GlobalIgnore = True Then Exit Sub
    ' If not on a channel, ignore.
    If currentChannel = "" Then
      IRCInterface.SendCTCPReply Nick, "PMCHATREQUEST", "CANT"
      Exit Sub
    End If
    ' check for ignore
    If GetWord(GetMask(Nick), 2) = "1" Then Exit Sub
    If modAC.GlobalIgnore = True Then IRCInterface.SendCTCPReply Nick, "PMCHATREQUEST", "DENY": Exit Sub
    If modAC.InPM <> "" Then
      IRCInterface.SendCTCPReply Nick, "PMCHATREQUEST", "CANT"
      Exit Sub
    End If
        cchck = MsgBox(Nick + " has requested a private chat with you. Do you want to accept?", vbYesNo + vbQuestion + vbDefaultButton2, "Private Conversation Request")
    If cchck = vbYes Then
      AddToHistory "Now entering private conversation with " + Nick, True
      IRCInterface.SendCTCPReply Nick, "PMCHATREQUEST", "ACCEPT"
      InPM = Nick
    Else
      IRCInterface.SendCTCPReply Nick, "PMCHATREQUEST", "DENY"
    End If
  Case "GI"
    If modAC.GlobalIgnore = True Then
      IRCInterface.SendCTCPReply Nick, "GI", "Y"
    ElseIf modAC.InPM <> "" Then
      IRCInterface.SendCTCPReply Nick, "GI", "P"
    Else
      IRCInterface.SendCTCPReply Nick, "GI", "N"
    End If
End Select
End Sub

Private Sub IRCInterface_CTCPReply(ByVal Command As String, ByVal Params As String, ByVal Nick As String, ByVal mask As String, ByVal Dest As String)
Select Case Command
  Case "PMCHATREQUEST"
    ' if not from the nick we requested, cancel
    If LCase(Nick) <> LCase(pcNick) Then Exit Sub
    Select Case Params
      Case "ACCEPT"
        AddToHistory "Now entering private conversation with " + Nick, True
        InPM = Nick
        pcNick = ""
      Case "CANT"
        AddToHistory Nick + " is already in a private conversation.", True
        pcNick = ""
      Case Else
        If GetWord(Params, 2) = "FLOOD" Then
          AddToHistory Nick & " can't accept a private chat request at this time. Please wait 30 seconds and try your request again.", True
        Else
          AddToHistory Nick + " declined your private chat request.", True
        End If
        pcNick = ""
    End Select
  Case "GI"
    Select Case Params
      Case "Y"
      AddToHistory Nick + " has global ignore turned on.", True
      Case "N"
      AddToHistory Nick + " does not have global ignore turned on.", True
      Case "P"
      AddToHistory Nick + " is in a private conversation.", True
      Case "S"
      AddToHistory Nick + " is currently playing a game of StarFight.", True
      Case "B"
      AddToHistory Nick + " is a Bot.", True
    End Select
End Select
End Sub

Private Sub IRCInterface_Debugger(ByVal DBGString As String)
If modAC.WriteDebugs = True Then Print #3, DBGString
End Sub

Private Sub IRCInterface_Disconnect(ByVal ServerName As String)
If NoReconnect = True Then
  IRCInterface.Disconnect
  Exit Sub
End If

Dim msg As Byte
currentChannel = ""
Me.Refresh
Me.Caption = SoftwareName + " version " + _
  Trim(Str(App.Major)) + "." + Trim(Str(App.Minor))
If App.Revision <> 0 Then Me.Caption = Me.Caption & _
  "." & CStr(App.Revision)
Me.Caption = Me.Caption & " " & Me.Tag
msg = MsgBox("You have been unexpectedly disconnected from the server. Do you wish to reconnect?", vbYesNo + vbCritical, "Connection Error")
If msg = vbYes Then
IRCInterface.Connect
Exit Sub
Else
Unload Me
End If

End Sub

Private Sub IRCInterface_errCantJoinBanned(ByVal Chan As String)
currentChannel = ""
MsgBox "You can't join " + Chan + " because you are banned.", vbCritical + vbOKOnly, "Unable to join"
End Sub

Private Sub IRCInterface_errCantJoinFull(ByVal Chan As String)
currentChannel = ""
MsgBox "You can't join " + Chan + " because the channel cannot accept any more members.", vbCritical + vbOKOnly, "Unable to join"
End Sub

Private Sub IRCInterface_errCantJoinMustRegister(ByVal Chan As String) ' added by MG bledig
currentChannel = ""
MsgBox SPrintF(lang.Messages(94), Chan), vbCritical + vbOKOnly, SPrintF(lang.Messages(93))
End Sub

Private Sub IRCInterface_errCantJoinInviteOnly(ByVal Chan As String)
currentChannel = ""
MsgBox SPrintF(lang.Messages(95), Chan), vbCritical + vbOKOnly, SPrintF(lang.Messages(93))
End Sub

Private Sub IRCInterface_errCantJoinKey(ByVal Chan As String)
currentChannel = ""
MsgBox SPrintF(lang.Messages(96), Chan), vbCritical + vbOKOnly, SPrintF(lang.Messages(93))
End Sub

Private Sub IRCInterface_errNickErroneus()
MsgBox SPrintF(lang.Messages(97)), vbOKOnly + vbCritical, SPrintF(lang.Messages(20))
Load frmNick
frmNick.formMode = 1
frmNick.Show vbModal, frmMain
IRCInterface.Nick = Settings.Nickname
IRCInterface.Linkup

End Sub

Private Sub IRCInterface_errNickInUse()
If IRCInterface.LinkedUp = True Then
MsgBox SPrintF(lang.Messages(98)), vbOKOnly + vbCritical, SPrintF(lang.Messages(20))
Exit Sub
End If
MsgBox SPrintF(lang.Messages(99)), vbOKOnly + vbCritical, SPrintF(lang.Messages(20))
Load frmNick
frmNick.formMode = 1
frmNick.Show vbModal, frmMain
IRCInterface.Nick = Settings.Nickname
IRCInterface.Linkup
End Sub

Private Sub IRCInterface_errNotChanOp()
If currentChannel <> "" Then
  AddToHistory "Sorry, you're not a channel operator!", True
End If
End Sub

Private Sub IRCInterface_Error(ByVal ErrorText As String)
' A few special error cases
If InStr(ErrorText, "Throttled") Then
  MsgBox "You have been disconnected from the chat server because you have been reconnecting too quickly. Please wait one hour and try again. If you believe there to be an error, please use the 'Send Feedback' menu item of the 'help' menu.", vbOKOnly + vbCritical, "Connection Throttled"
  Unload Me
 End
  End If
  'If InStr(ErrorText, "banned") Then
'MsgBox "You have been banned from this server. If you believe this is an error, please contact irc@talkingirc.com.", vbCritical, "You are Banned"
'Unload Me
'End
'End If

End Sub

Private Sub IRCInterface_errUnrealCantJoinOpsOnly()
currentChannel = ""
MsgBox "You can't join that channel because it is for network administrators only.", vbCritical + vbOKOnly, "Unable to join"
End Sub

Private Sub IRCInterface_errUnrealCantJoinSecureOnly()
currentChannel = ""
MsgBox "You can't join that channel because it is a secured channel.", vbCritical + vbOKOnly, "Unable to join"

End Sub

Private Sub IRCInterface_Generic(ByVal Command As String, ByVal Data As String, ByVal Comment As String)
Select Case Command
  Case "301"
    AddToHistory "Notice: " & GetWord(Data, 2) & " is currently marked as being away.", True
  Case "600", "604", "605", "601"
    watchesProc = watchesProc - 1
End Select
End Sub

Private Sub IRCInterface_GotOps(ByVal Nick As String, ByVal Chan As String, ByVal Opper As String)
AddToHistory Nick + " is now a channel host! (opped by " + Opper + ")", True
RemoveFromUsers Nick
lstUsers.AddItem "@" + Nick
End Sub

Private Sub IRCInterface_GotHalfOps(ByVal Nick As String, ByVal Chan As String, ByVal Opper As String) ' Added by MG Bledig
AddToHistory Nick + " now has half ops! (half opped by " + Opper + ")", True
RemoveFromUsers Nick
lstUsers.AddItem "%" + Nick
End Sub

Private Sub IRCInterface_GotAdminOps(ByVal Nick As String, ByVal Chan As String, ByVal Opper As String) ' Added by MG Bledig
AddToHistory Nick + " now has admin ops! (admin opped by " + Opper + ")", True
RemoveFromUsers Nick
lstUsers.AddItem "&" + Nick
End Sub

Private Sub IRCInterface_GotOwnerOps(ByVal Nick As String, ByVal Chan As String, ByVal Opper As String) ' Added by MG Bledig
AddToHistory Nick + " now has Owner ops! (owner opped by " + Opper + ")", True
RemoveFromUsers Nick
lstUsers.AddItem "~" + Nick
End Sub

Private Sub IRCInterface_GotVoice(ByVal Nick As String, ByVal Chan As String, ByVal Opper As String) ' Added by MG Bledig
AddToHistory Nick + " now has voice! (voiced by " + Opper + ")", True
RemoveFromUsers Nick
lstUsers.AddItem "+" + Nick
End Sub

Private Sub IRCInterface_Invite(ByVal Channel As String, ByVal Nick As String, ByVal mask As String)
Dim GoJoin As Byte


If currentChannel = "" Then
      GoJoin = MsgBox("Invitation dialog. " & Nick & " has invited you to talk in " & Channel & vbCrLf + "Do you want to join now?", vbYesNo + vbQuestion, "Invitation")
  If GoJoin = vbYes Then currentChannel = Channel
Else
  InviteChan = Channel
  AddToHistory "Invitation! " & Nick & " has invited you to talk in " & Channel & "! To accept this invitation, type /accept into the text entry and press Enter. You will automatically leave the room you're in and join " & Channel & ".", True
End If
End Sub

Private Sub IRCInterface_IRCDebugger(ByVal IRCString As String, ByVal Direction As String)
If modAC.WriteDebugs = True Then Print #4, Direction + ": " + IRCString
'Debug.Print IRCString
End Sub

Private Sub IRCInterface_IRCQuit(ByVal Reason As String, ByVal Nick As String, ByVal mask As String)
modSound.PlaySound App.Path + "\sounds\PART.WAV"
Select Case LCase(Reason)
  Case "connection reset by peer", "ping timeout"
    AddToHistory Nick + " has lost their connection to the server!", True
  Case "broken pipe"
    AddToHistory Nick + " experienced a network error!", True
  Case Else
    AddToHistory Nick + " has left the server!", True
End Select

RemoveFromUsers Nick
lblMembers = Trim(Str(lstUsers.ListCount)) + " members"

End Sub

Private Sub IRCInterface_Join(ByVal Channel As String, ByVal Nick As String, ByVal mask As String)
theTopic = ""
If LCase(Nick) = LCase(IRCInterface.Nick) And Settings.AutoLog = 1 Then
  Close #2
  Open App.Path + "\Transcripts\" + Channel + ".log" For Append As #2
  Print #2, ""
  Print #2, "Log start: " + CStr(Now)
End If

' SAJOIN Handler
If LCase(currentChannel) <> LCase(Channel) Then
  If LCase(Nick) = LCase(IRCInterface.Nick) Then
  currentChannel = Channel
  AddToHistory "You were just forced to join " & Channel
  Exit Sub
  End If
End If

' look for us
If LCase(IRCInterface.Nick) = LCase(Nick) Then
  AddToHistory "You are now talking on " & Channel & "!"
Else
  AddToHistory Nick + " has joined the room!", True
End If
lstUsers.AddItem Nick
lblMembers = Trim(Str(lstUsers.ListCount)) + " members"
If modAC.InPM <> "" Then Exit Sub
modSound.PlaySound App.Path + "\sounds\JOIN.WAV", False
' Check for Private mode set
If modAC.OnJoinSetPriv = True And LCase(Nick) = LCase(IRCInterface.Nick) Then
  ' set mode
  IRCInterface.RawSend "MODE " + Channel + " +p"
  modAC.OnJoinSetPriv = False
End If
End Sub

Private Sub IRCInterface_Kick(ByVal Channel As String, ByVal Nick As String, ByVal Kicker As String, ByVal KickerMask As String, ByVal Reason As String)
modSound.PlaySound App.Path + "\sounds\KICK.WAV"
If LCase(Reason) = LCase(Kicker) Then Reason = "(No Reason Given)"
If LCase(Nick) = LCase(IRCInterface.Nick) Then
  currentChannel = ""
    MsgBox "You were kicked from " + Channel + " by " + Kicker + " because: " & Reason, vbCritical + vbOKOnly, "Alert"
  Exit Sub
End If
AddToHistory Nick + " was kicked from the room by " + Kicker + " because: " & Reason & "!", True

RemoveFromUsers Nick

lblMembers = Trim(Str(lstUsers.ListCount)) + " members"

End Sub

Private Sub IRCInterface_Kill()
If Settings.UseSounds = True Then modSound.PlaySound App.Path + "\sounds\KICK.WAV", True
Dim oForm As Form

If NoReconnect = False Then
  MsgBox "You were just disconnected from Nightowl by a network administrator. If you are repeatedly receiving this message upon connecting, you are probably banned. Please contact an administrator for help.", vbOKOnly + vbCritical, "Oh No!"
  NoReconnect = True
  For Each oForm In Forms
  Unload oForm
  Next
  End
End If
End Sub

Private Sub IRCInterface_LostOps(ByVal Nick As String, ByVal Chan As String, ByVal Opper As String)
AddToHistory Nick + " is no longer a channel host! (de-opped by " + Opper + ")", True
RemoveFromUsers Nick
lstUsers.AddItem Nick
End Sub

Private Sub IRCInterface_LostHalfOps(ByVal Nick As String, ByVal Chan As String, ByVal Opper As String)
AddToHistory Nick + " no longer has half ops! (de-half opped by " + Opper + ")", True
RemoveFromUsers Nick
lstUsers.AddItem Nick
End Sub

Private Sub IRCInterface_LostAdminOps(ByVal Nick As String, ByVal Chan As String, ByVal Opper As String)
AddToHistory Nick + " no longer has admin ops! (de-admin opped by " + Opper + ")", True
RemoveFromUsers Nick
lstUsers.AddItem Nick
End Sub

Private Sub IRCInterface_LostOwnerOps(ByVal Nick As String, ByVal Chan As String, ByVal Opper As String)
AddToHistory Nick + " no longer has owner ops! (de-half opped by " + Opper + ")", True
RemoveFromUsers Nick
lstUsers.AddItem Nick
End Sub

Private Sub IRCInterface_LostVoice(ByVal Nick As String, ByVal Chan As String, ByVal Opper As String) ' Added by MG Bledig
AddToHistory Nick + " no longer has voice! (de-voiced by " + Opper + ")", True
RemoveFromUsers Nick
lstUsers.AddItem Nick
End Sub

Private Sub IRCInterface_MOTD(ByVal MOTD As String)
Dim motdDate, motdDateCompare  As String

' Retrieve the date of the MOTD
motdDate = GetLine(MOTD, 1)

' Open the MOTD file
On Error GoTo NoFile
Open App.Path + "\MOTD.DAT" For Input As #1
Line Input #1, motdDateCompare

If GetWord(motdDate, 1) = GetWord(motdDateCompare, 1) Then Close #1: Exit Sub

Close #1

NoFile:
Open App.Path + "\MOTD.DAT" For Output As #1
Print #1, MOTD
Close #1

mnuMOTD_Click

End Sub

Private Sub IRCInterface_NamesList(NamesList() As String, Channel As String)
lstUsers.Clear

For c = 1 To UBound(NamesList)
  If Len(NamesList(c)) > 0 Then
    Select Case Left$(NamesList(c), 1)
    Case "@"
      lstUsers.AddItem NamesList(c)
    
    Case "~"  ' Added by MG Bledig
                              lstUsers.AddItem NamesList(c)
        
    Case "&"  ' Added by MG Bledig
                              lstUsers.AddItem NamesList(c)
        
    Case "%" ' Added by MG Bledig
      lstUsers.AddItem NamesList(c)
        
        Case "+" ' Added by MG Bledig
      lstUsers.AddItem NamesList(c)

    Case Else
    lstUsers.AddItem TrimNonAlpha(NamesList(c))
    End Select
  End If
Next

lblMembers = Trim(Str(lstUsers.ListCount)) + " members"
End Sub

Private Sub IRCInterface_Nick(ByVal OldNick As String, ByVal NewNick As String)
AddToHistory OldNick + " is now known as " + NewNick, True
RemoveFromUsers OldNick
lstUsers.AddItem NewNick

If LCase(OldNick) = LCase(IRCInterface.Nick) Then
  IRCInterface.Nick = NewNick
  Settings.Nickname = NewNick
Me.Caption = SoftwareName + " version " + _
  Trim(Str(App.Major)) + "." + Trim(Str(App.Minor))
If App.Revision <> 0 Then Me.Caption = Me.Caption & _
  "." & CStr(App.Revision)
Me.Caption = Me.Caption & "  Nickname is" + Settings.Nickname
ircServs.nsIdentified = False
End If

End Sub

Private Sub IRCInterface_Notice(ByVal Text As String, ByVal Nick As String, ByVal mask As String, ByVal Dest As String)
  If modAC.currentChannel = "" Then Exit Sub
AddToHistory "Notice from " & Nick & ": " & StripCodes(Text), True
End Sub

Private Sub IRCInterface_Part(ByVal Channel As String, ByVal Nick As String, ByVal mask As String)
  
modSound.PlaySound App.Path + "\sounds\PART.WAV", False

If LCase(currentChannel) = LCase(Channel) And LCase(Nick) = LCase(IRCInterface.Nick) Then
  currentChannel = ""
  If Settings.AutoLog = 1 Then Close #2
Else
  ' If the part was for us, but not on the current room, it's a
  ' stray part; ignore it.
  If LCase(Nick) = LCase(IRCInterface.Nick) Then Exit Sub
  
  RemoveFromUsers Nick
  lblMembers = Trim(Str(lstUsers.ListCount)) + " members"
  If modAC.InPM <> "" Then Exit Sub
  AddToHistory Nick + " has left the room!", True
End If

End Sub

Private Sub IRCInterface_PrivMsg(ByVal Text As String, ByVal Nick As String, ByVal mask As String, ByVal Dest As String)
' check for ignore
If GetWord(GetMask(Nick), 2) = "1" Then Exit Sub
' if not on channel ignore
If currentChannel = "" Then Exit Sub

If modAC.InPM = "" Then
  SndFloodCounter = SndFloodCounter + 1
  ' 5 sounds per 30 secs
  If SndFloodCounter < 6 Then modSound.PlaySound App.Path + "\sounds\PRIVATE.WAV", False
End If
DoEvents
AddToHistory "private " + Nick + " says " + Text, True
End Sub

Private Sub IRCInterface_SignedIn(ByVal Details As String)
' Set our status bar to show we're connected
sBar.Panels(1).Text = "Connected!"
' Play the Connected sound
If Settings.UseSounds = 1 Then modSound.PlaySound App.Path + "\sounds\CONNECT.WAV", True
' speak connected message
SpeechInterface.Say lang.SpeechMsg(5)

' set window caption
Me.Caption = Me.Caption & "  Nickname is " + Settings.Nickname

' send the watch list
modAC.sendWatch

' display the channel list
mnuList_Click

' Set floodflag to true for initial run
FloodFlag = True
FloodProtector.Enabled = True

End Sub

Private Sub IRCInterface_SysError(ByVal ErrorText As String, ByVal Fatal As Boolean)
If InStr(ErrorText, "forcefully rejected") Then
MsgBox "Your attempt to connect was rejected. This usually occurs when the server is not available. Please try connecting again later.", vbCritical, "Connection Rejected"
Unload Me
End If
If Settings.UseSounds = 1 Then
  modSound.PlaySound App.Path + "\sounds\PROBLEM.WAV", False
    MsgBox "Fatal system error:" + vbCrLf + ErrorText + vbCrLf + "Please submit a bug report using the 'Send Feedback' menu item of the 'Help' menu.", vbOKOnly + vbCritical, "Run...Run away Scar, and never return..."
  Unload Me
End If
End Sub

Private Sub IRCInterface_Topic(ByVal Channel As String, ByVal TopicText As String)
AddToHistory "The topic for channel " & Channel & " is: " & TopicText, True
theTopic = TopicText
End Sub

Private Sub IRCInterface_UserHost(ByVal Nick As String, ByVal host As String)
HostBuffer = host
End Sub

Private Sub IRCInterface_WatchOff(ByVal Nick As String)
If Settings.WSoundOn = True Then
  modSound.PlaySound App.Path & "\sounds\SIGNOFF.WAV", True
End If
If Settings.WAlert = True Then
  If modAC.currentChannel = "" Then
    SpeechInterface.Say SPrintF(lang.SpeechMsg(6), Nick)
  Else
    AddToHistory SPrintF(lang.SpeechMsg(6), Nick), True
  End If
End If

End Sub

Private Sub IRCInterface_WatchOn(ByVal Nick As String)

If Settings.WSoundOn = True Then
  modSound.PlaySound App.Path & "\sounds\SIGNON.WAV", True
End If
  If modAC.currentChannel = "" Then
    SpeechInterface.Say SPrintF(lang.SpeechMsg(7), Nick)
      Else
    AddToHistory SPrintF(lang.SpeechMsg(7), Nick), True
  End If

End Sub










Private Sub lstUsers_KeyUp(KeyCode As Integer, Shift As Integer)

If lstUsers.ListIndex = -1 Then Exit Sub
Dim DestNick As String
Dim kRes As String
Dim cnfm As Byte

DestNick = TrimNonAlpha(lstUsers.List(lstUsers.ListIndex))

' F4 - the kick command
If KeyCode = vbKeyF4 And Shift = 0 Then
  If IsChanop = False Then
    MsgBox "You must be a channel operator to kick users.", vbOKOnly Or vbCritical, "Only for Channel Hosts"
    Exit Sub
  End If
  kRes = InputBox("Please input a reason for kicking " & DestNick & ".", "Kick Reason")
  IRCInterface.SendKick currentChannel, DestNick, kRes
  txtInput.Text = ""
  txtInput.SetFocus
End If

'Shift+F3 get whois information (ace)
If KeyCode = vbKeyF3 And Shift = vbShiftMask Then
frmWhois.DoWhois TrimNonAlpha(frmMain.lstUsers.List(frmMain.lstUsers.ListIndex))
Exit Sub
End If

' Shift+F4 - the ban command
If KeyCode = vbKeyF4 And Shift = vbShiftMask Then
  HostBuffer = ""
  IRCInterface.RawSend "USERHOST " + TrimNonAlpha(DestNick)
  Do Until HostBuffer <> ""
    DoEvents
  Loop
  AddToHistory "Banning " + DestNick + " (hostname *!*@" + GetFromWord(HostBuffer, 2, "@") + ")", True
  IRCInterface.RawSend "MODE " + currentChannel + " +b *!*@" + GetFromWord(HostBuffer, 2, "@")
  IRCInterface.RawSend "KICK " + currentChannel + " " + DestNick
End If

' Shift F8 - give or take voice - by MG Bledig
If KeyCode = vbKeyF8 And Shift = vbShiftMask Then
If LCase(lstUsers.List(lstUsers.ListIndex)) = "+" + LCase(IRCInterface.Nick) Then
    cnfm = MsgBox("Are you absolutely sure you want to revoke your channel voice status?", vbYesNo + vbDefaultButton2 + vbInformation, "Are You Sure?")
  If cnfm = vbNo Then Exit Sub
End If
If Left(lstUsers.List(lstUsers.ListIndex), 1) = "+" Then
  IRCInterface.RawSend "MODE " + currentChannel + " -v " + DestNick
Else
  IRCInterface.RawSend "MODE " + currentChannel + " +v " + DestNick
End If
txtInput.SetFocus
End If

' F8 - give or take ops
If KeyCode = vbKeyF8 And Shift = 0 Then
If LCase(lstUsers.List(lstUsers.ListIndex)) = "@" + LCase(IRCInterface.Nick) Then
    cnfm = MsgBox("Are you absolutely sure you want to revoke your channel op status? You will be unable to re-gain it on your own.", vbYesNo + vbDefaultButton2 + vbInformation, "Are You Sure?")
  If cnfm = vbNo Then Exit Sub
End If
If Left(lstUsers.List(lstUsers.ListIndex), 1) = "@" Then
  IRCInterface.RawSend "MODE " + currentChannel + " -o " + DestNick
Else
  IRCInterface.RawSend "MODE " + currentChannel + " +o " + DestNick
End If
txtInput.SetFocus
End If

' F9 - request a private chat
If KeyCode = vbKeyF9 Then
  If LCase(DestNick) = LCase(IRCInterface.Nick) Then _
        MsgBox "You can't request to chat with yourself!", vbOKOnly + vbCritical, "Error": Exit Sub
    cnfm = MsgBox("You are requesting a private chat with " + DestNick + ". Is this what you want?", vbYesNo + vbQuestion, "Private Conversation Request")
  If cnfm = vbYes Then
    IRCInterface.SendCTCP DestNick, "PMCHATREQUEST", ""
    pcNick = DestNick
  End If
End If

' F11 - Prefix nickname command
If KeyCode = vbKeyF11 And Shift = 0 Then
  txtInput.Text = TrimNonAlpha(DestNick) + ", " + txtInput.Text
  IRCInterface.SendMessage currentChannel, txtInput.Text
  AddToHistory IRCInterface.Nick + " says " + txtInput.Text, True
  txtInput.Text = ""
  txtInput.SetFocus
End If

' Shift F11 - Check GI Status
If KeyCode = vbKeyF11 And Shift = vbShiftMask Then
  IRCInterface.SendCTCP DestNick, "GI", ""
  txtInput.SetFocus
End If

' F12 - Ignore function
If KeyCode = vbKeyF12 And Shift = 0 Then
  If LCase(DestNick) = LCase(IRCInterface.Nick) Then _
    MsgBox "You can't ignore yourself!", vbOKOnly + vbCritical, "Error": Exit Sub
  frmIgnore.SetupForNick DestNick
End If

'sending a message to an user
If KeyCode = vbKeyReturn Then
SendInputBuffer
End If

End Sub


Private Sub mnuAbout_Click()
frmAbout.Show
End Sub

Private Sub mnuAutoLog_Click()
Select Case mnuAutoLog.Checked
Case False
  Settings.AutoLog = 1
  If currentChannel <> "" Then
    Open App.Path + "\transcripts\" + currentChannel + ".log" For Append As #2
    Print #2, vbCrLf
    Print #2, "Log start: " + CStr(Now)
  End If
  mnuAutoLog.Checked = True
Case True
  Settings.AutoLog = 0
  Close #2
  mnuAutoLog.Checked = False
End Select
End Sub


Private Sub mnuCExit_Click()
mnuCExit.Checked = Not mnuCExit.Checked
Settings.ConfirmExit = mnuCExit.Checked

End Sub

Private Sub mnuCHNick_Click()
Load frmNick
frmNick.formMode = 3
frmNick.Show
End Sub

Private Sub mnuClearHistory_Click()
SpeechInterface.Say lang.SpeechMsg(8), True
txtHistory.Text = ""
End Sub

Private Sub mnuClearIgnore_Click()
cfm = MsgBox("This will clear all user specific ignore options you have set. Are you sure?", vbYesNo + vbDefaultButton2 + vbQuestion, "Clear Ignores")
If cfm = vbYes Then lstIgnore.Clear: AddToHistory "User ignores cleared", True
End Sub

Private Sub MNUDisconnect_Click()

Disconnect

End Sub

Private Sub mnuExit_Click()
Unload Me
End Sub



Private Sub MNUFb_Click()

FrmFeedback.Show vbModal, frmMain

End Sub

Private Sub mnuGetBan_Click()
frmBan.Show
End Sub

Private Sub mnuGIgnore_Click()
  Select Case modAC.GlobalIgnore
  Case False
    Settings.UseSounds = 0
    modAC.GlobalIgnore = True
    lblIgnore = "Global Ignore Enabled"
    IRCInterface.SendMessage currentChannel, "Global Ignore is now enabled; all speech output is disabled."
    SpeechInterface.Say lang.SpeechMsg(9), True
    mnuGIgnore.Checked = True
  Case True
    If mnuSound.Checked = True Then Settings.UseSounds = 1
    modAC.GlobalIgnore = False
    lblIgnore = ""
    IRCInterface.SendMessage currentChannel, "Global Ignore is no longer enabled."
    SpeechInterface.Say lang.SpeechMsg(10), True
    mnuGIgnore.Checked = False
      End Select

End Sub

Private Sub mnuHelp_Click()
Shell "hh.exe " & App.Path & "\Freedom.CHM", vbNormalFocus
End Sub

Private Sub mnuJoin_Click()
frmCreateChan.Show
End Sub

Private Sub mnuLarge_Click(Index As Integer)
Dim oMnu As Menu

For Each oMnu In mnuLarge
  oMnu.Checked = False
Next

Select Case Index
  Case 0
    With txtHistory.Font
      .Name = "MS Sans Serif"
      .Bold = False
      .Size = 8
    End With
    With txtInput.Font
      .Name = "MS Sans Serif"
      .Bold = False
      .Size = 8
    End With
    With lstUsers.Font
      .Name = "MS Sans Serif"
      .Bold = False
      .Size = 8
    End With
    mnuLarge(0).Checked = True
    Settings.LargePrint = 0
  Case 1
    With txtHistory.Font
      .Name = "Tahoma"
      .Bold = True
      .Size = 14
    End With
    With txtInput.Font
      .Name = "Tahoma"
      .Bold = True
      .Size = 12
    End With
    With lstUsers.Font
      .Name = "Tahoma"
      .Bold = True
      .Size = 10
    End With
    lstUsers.Height = Me.Height - 1875
    mnuLarge(1).Checked = True
    Settings.LargePrint = 1
  Case 2
    With txtHistory.Font
      .Name = "Tahoma"
      .Bold = True
      .Size = 18
    End With
    With txtInput.Font
      .Name = "Tahoma"
      .Bold = True
      .Size = 14
    End With
    With lstUsers.Font
      .Name = "Tahoma"
      .Bold = True
      .Size = 14
    End With
    lstUsers.Height = Me.Height - 1875
    mnuLarge(2).Checked = True
    Settings.LargePrint = 2
  Case 3
    With txtHistory.Font
      .Name = "Tahoma"
      .Bold = True
      .Size = 24
    End With
    With txtInput.Font
      .Name = "Tahoma"
      .Bold = True
      .Size = 18
    End With
    With lstUsers.Font
      .Name = "Tahoma"
      .Bold = True
      .Size = 16
    End With
    lstUsers.Height = Me.Height - 1875
    mnuLarge(3).Checked = True
    Settings.LargePrint = 3

End Select


End Sub



Private Sub mnuList_Click()
frmChList.Show
End Sub

Private Sub mnuMOTD_Click()

Dim MOTD As String
Dim motdDateCompare As String

' Open the MOTD file
On Error GoTo NoFile
Open App.Path + "\MOTD.DAT" For Input As #1

Do Until EOF(1)
Line Input #1, motdDateCompare
MOTD = MOTD + motdDateCompare + vbCrLf
Loop

Close #1

Load frmMOTD
frmMOTD.txtMotd.Text = MOTD
frmMOTD.Show vbModal, frmMain
SpeechInterface.StopSpeech

NoFile:
Exit Sub

End Sub


Private Sub mnuOWatch_Click()
frmWatchCfg.Show vbModal, frmMain
End Sub

Private Sub mnuPart_Click()
If modAC.InPM <> "" Then
  AddToHistory "Ending private conversation with " + modAC.InPM
  IRCInterface.SendCTCP modAC.InPM, "LEAVINGPMCHAT", ""
  modAC.InPM = ""
  Exit Sub
End If
currentChannel = ""
End Sub


Private Sub mnuSAPI_Click()
frmSAPI.Show
End Sub

Private Sub mnuSave_Click()
cdg1.CancelError = False
cdg1.DialogTitle = "Save Transcript"
cdg1.InitDir = App.Path + "\Transcripts"
cdg1.Flags = cdlOFNHideReadOnly Or cdlOFNOverwritePrompt
cdg1.Filter = "Text Files (*.txt)|*.txt"
cdg1.ShowSave

If cdg1.FileName = "" Then Exit Sub

txtHistory.SaveFile cdg1.FileName, rtfText
MsgBox "Transcript saved.", vbOKOnly + vbInformation, "Save Transcript"


End Sub

Private Sub MNUServList_Click()

FrmServerList.Show vbModal, frmMain

End Sub

Private Sub mnuSound_Click()
Select Case mnuSound.Checked
  Case True
    modAC.Settings.UseSounds = 0
    mnuSound.Checked = False
    SpeechInterface.Say lang.SpeechMsg(11)
  Case False
    modAC.Settings.UseSounds = 1
    mnuSound.Checked = True
    SpeechInterface.Say lang.SpeechMsg(12)
End Select
End Sub

Private Sub mnuSTopic_Click()
If IsChanop = False Then
  MsgBox lang.Messages(66), vbOKOnly Or vbCritical, lang.Messages(67)
  Exit Sub
End If
Dim nTopic As String

nTopic = InputBox("Enter the new channel topic for " & modAC.currentChannel, "Set Channel Topic", theTopic)
If nTopic = "" Then Exit Sub
IRCInterface.RawSend "TOPIC " & modAC.currentChannel & " :" & nTopic
AddToHistory "Changing the topic for " & modAC.currentChannel & " to: " & nTopic
End Sub

Private Sub mnuSWatch_Click()
If watchesProc > 0 Then
  MsgBox "Your watch list is still being processed - please try this command again in a few moments.", vbOKOnly Or vbCritical, "Please Wait"
  Exit Sub
End If
frmWatch.Show

End Sub

Private Sub mnuSynth_Click()
frmSpeech.Show vbModal, frmMain
SpeechInterface.Engine = Settings.SpeechType
SpeechInterface.Initialize
SpeechInterface.Say lang.SpeechMsg(13), True
If SpeechInterface.Engine = 5 Then
mnuSAPI.Enabled = True
Else
mnuSAPI.Enabled = False
End If

End Sub


Private Sub mnuWeb_Click()
modAC.OpenLocation homepage, 3
End Sub

Private Sub mnuWhois_Click()
Dim wnick As String

wnick = InputBox("Enter nickname to perform Whois on", "Whois")
If wnick = "" Then Exit Sub
frmWhois.DoWhois wnick
End Sub

Private Sub mnuZTI_Click()
frmZTI.Show
End Sub





Private Sub txtHistory_GotFocus()
txtHistory.SelStart = Len(txtHistory)
SpeechInterface.StopSpeech
SpeechInterface.Say lang.SpeechMsg(14), True
End Sub

Private Sub txtHistory_Keydown(KeyCode As Integer, Shift As Integer)

If KeyCode = vbKeyEscape Then
mnuClearHistory_Click
ClearHistoryBufferKeys ' Clear the History BufferKeys, it makes sense to, right?
End If

End Sub


Private Sub txtHistory_KeyPress(KeyAscii As Integer)

If KeyAscii > 30 Then
txtInput.Text = txtInput.Text & Chr(KeyAscii)
txtInput.SetFocus
txtInput.SelStart = Len(txtInput)
 End If
 
End Sub

Private Sub txtInput_Change()
' Alert user if text field could be truncated upon send.
If Len(txtInput.Text) > 256 Then modSound.Ding
End Sub

Private Sub txtInput_GotFocus()

txtHistory.SelStart = Len(txtHistory.Text)
SpeechInterface.Say lang.SpeechMsg(15), True
End Sub

Private Sub txtInput_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyEscape Then
txtInput.Text = ""
SpeechInterface.Say lang.SpeechMsg(19), True
ClearInputBuffer
End If


If KeyCode = vbKey1 And Shift = vbAltMask Then
SpeakHistoryBuffer 0
ElseIf KeyCode = vbKey2 And Shift = vbAltMask Then
SpeakHistoryBuffer 1
ElseIf KeyCode = vbKey3 And Shift = vbAltMask Then
SpeakHistoryBuffer 2
ElseIf KeyCode = vbKey4 And Shift = vbAltMask Then
SpeakHistoryBuffer 3
ElseIf KeyCode = vbKey5 And Shift = vbAltMask Then
SpeakHistoryBuffer 4
ElseIf KeyCode = vbKey6 And Shift = vbAltMask Then
SpeakHistoryBuffer 5
ElseIf KeyCode = vbKey7 And Shift = vbAltMask Then
SpeakHistoryBuffer 6
ElseIf KeyCode = vbKey8 And Shift = vbAltMask Then
SpeakHistoryBuffer 7
ElseIf KeyCode = vbKey9 And Shift = vbAltMask Then
SpeakHistoryBuffer 8
ElseIf KeyCode = vbKey0 And Shift = vbAltMask Then
SpeakHistoryBuffer 9
End If

If modAC.GlobalIgnore = True Then Exit Sub
If KeyCode = vbKeyM And Shift = vbCtrlMask Then
'Fix: check if there is actually an action before sending! (ace)
  If txtInput.Text = "" Then Exit Sub
    IRCInterface.SendCTCP currentChannel, "ACTION", txtInput.Text
    AddToHistory IRCInterface.Nick + " " + txtInput.Text, True
    txtInput.Text = ""
End If
If KeyCode = vbKeyF10 And txtInput.Text <> "" Then KeyCode = 0: Load frmUsers: frmUsers.formMode = 0: frmUsers.FormSet: frmUsers.Show
If KeyCode = vbKeyReturn And Shift = 0 Then
'add it to the inputBuffer for later use
Call AddToBuffer(txtInput.Text, InputBuffer)
SendInputBuffer
End If

'History of the last 10 sent messages
If KeyCode = vbKeyUp Then
txtInput.Text = SelectIBS(0)
ElseIf KeyCode = vbKeyDown Then
txtInput.Text = SelectIBS(1)
ElseIf KeyCode = vbKeyReturn And Shift = vbShiftMask Then
If InputBuffer(0) <> "" Then
txtInput.Text = InputBuffer(0)
SendInputBuffer
End If
End If
 
End Sub



' -- IGNORE --
Sub AddIgnore(ByVal IgnoreMask As String)
lstIgnore.AddItem IgnoreMask
End Sub

Sub DelIgnore(ByVal IgnoreNick As String)
For c = 0 To lstIgnore.ListCount
  If LCase(GetWord(lstIgnore.List(c), 1)) = LCase(IgnoreNick) Then
    lstIgnore.RemoveItem c
  End If
Next
End Sub

Function GetMask(ByVal IgnoreNick As String)
For c = 0 To lstIgnore.ListCount
  If LCase(GetWord(lstIgnore.List(c), 1)) = LCase(IgnoreNick) Then
    GetMask = GetFromWord(lstIgnore.List(c), 2)
  End If
Next
If GetMask = "" Then GetMask = "0 0 0 0 0"
End Function

Sub RemoveFromUsers(ByVal Nick As String)
For c = 0 To lstUsers.ListCount - 1
  If LCase(Nick) = TrimNonAlpha(LCase(lstUsers.List(c))) Then _
    lstUsers.RemoveItem c: Exit For
  If LCase(Nick) = "@" + TrimNonAlpha(LCase(lstUsers.List(c))) Then _
    lstUsers.RemoveItem c: Exit For
Next
End Sub


Public Sub SpeakHistoryBuffer(Index As Byte)
Dim result As Long

If BufferTimer > 0 Then
result = GetTickCount
If (result - BufferTimer) < 500 And (bufferIndex = Index) Then
BufferCopy (Index)
bufferIndex = 0
Exit Sub
Else
BufferTimer = GetTickCount
End If
Else
BufferTimer = GetTickCount
End If

If HistoryBuffer(Index) = "" Then
SpeechInterface.Say lang.SpeechMsg(16), True
Else
SpeechInterface.Say HistoryBuffer(Index), True
End If
bufferIndex = Index
End Sub

Public Sub AddToBuffer(What As String, ByRef Dest() As String)
Dim i As Integer


For i = 8 To 0 Step -1
Dest(i + 1) = Dest(i)
Next i

Dest(0) = What

End Sub

Public Sub BufferCopy(What As Byte)

If HistoryBuffer(What) = "" Then
SpeechInterface.Say lang.SpeechMsg(17), True
Else
Clipboard.Clear
Clipboard.SetText HistoryBuffer(What)
SpeechInterface.Say SPrintF(lang.SpeechMsg(18), HistoryBuffer(What)), True
End If
BufferTimer = 0

End Sub

Private Sub ClearHistoryBufferKeys()

' clear the history buffers since the history window has been cleared
' MG bledig - Increjuler

Dim i As Integer ' array index

For i = 9 To 0 Step -1
HistoryBuffer(i) = "" ' clear each buffer
Next i

End Sub

Private Sub SetMenuCaptions()
mnu1File.Caption = lang.MenuMsg(0)
mnuClearHistory.Caption = lang.MenuMsg(1)
mnuSave.Caption = lang.MenuMsg(2)
mnuExit.Caption = lang.MenuMsg(3)
mnu1Channel.Caption = lang.MenuMsg(4)
mnuList.Caption = lang.MenuMsg(5)
mnuJoin.Caption = lang.MenuMsg(6)
mnuPart.Caption = lang.MenuMsg(7)
mnuGetBan.Caption = lang.MenuMsg(8)
mnuSTopic.Caption = lang.MenuMsg(9)
mnuCHNick.Caption = lang.MenuMsg(10)
mnuWhois.Caption = lang.MenuMsg(11)
mnuGIgnore.Caption = lang.MenuMsg(12)
mnuClearIgnore.Caption = lang.MenuMsg(13)
mnuZTI.Caption = lang.MenuMsg(14)
mnu1Options.Caption = lang.MenuMsg(15)
mnuSynth.Caption = lang.MenuMsg(16)
mnuSAPI.Caption = lang.MenuMsg(17)
mnuSound.Caption = lang.MenuMsg(18)
mnuAutoLog.Caption = lang.MenuMsg(19)
mnuCExit.Caption = lang.MenuMsg(20)
mnuLargeSub.Caption = lang.MenuMsg(21)
mnuLarge(0).Caption = lang.MenuMsg(22)
mnuLarge(1).Caption = lang.MenuMsg(23)
mnuLarge(2).Caption = lang.MenuMsg(24)
mnuLarge(3).Caption = lang.MenuMsg(25)
mnuSWatch.Caption = lang.MenuMsg(26)
mnuOWatch.Caption = lang.MenuMsg(27)
mnu1Help.Caption = lang.MenuMsg(28)
mnuHelp.Caption = lang.MenuMsg(29)
mnuWeb.Caption = lang.MenuMsg(30)
mnuMOTD.Caption = lang.MenuMsg(31)
mnuAbout.Caption = lang.MenuMsg(32)
MNU1Server.Caption = lang.MenuMsg(33)
MNUServList.Caption = lang.MenuMsg(34)
MNUDisconnect.Caption = lang.MenuMsg(35)
MNUDisconnect.Enabled = False
MNU1User.Caption = lang.MenuMsg(36)
MNUFb.Caption = lang.MenuMsg(37)

End Sub

Public Sub Connect(host As String, Port As Integer)

modSound.PlaySound App.Path + "\sounds\WORKING.WAV"
IRCInterface.IRCServer = host
IRCInterface.IRCPort = Port

IRCInterface.UserIdent = Settings.MyIdent
IRCInterface.FullName = Settings.MyName
IRCInterface.AutoPingReply = True
IRCInterface.AutoSignin = True
IRCInterface.Nick = Settings.Nickname
' Get our services object
Set ircServs = New clsServices
ircServs.Associate IRCInterface

IRCInterface.Connect
currentChannel = ""


End Sub

Public Sub Disconnect()

If IRCInterface.Connected = True Then
  IRCInterface.Delink Settings.QuitMsg
End If
MNUDisconnect.Enabled = False
MNUServList.Enabled = True

End Sub

Private Function SelectIBS(dir As Byte) As String 'Moves and returns to a specified index in the input buffer
Select Case dir
Case Is = 0 ' up
If InpIndex = UBound(InputBuffer) Then
InpIndex = -1
SelectIBS = ""
Else
 If InputBuffer(InpIndex + 1) <> "" Then
 InpIndex = InpIndex + 1
SelectIBS = InputBuffer(InpIndex)
Else
If InpIndex <> -1 Then SelectIBS = InputBuffer(InpIndex)
End If
End If
Case Is = 1 ' down
If InpIndex <= LBound(InputBuffer) Then
InpIndex = -1
'we're at the bottom, so return ""
SelectIBS = ""
Else
InpIndex = InpIndex - 1
SelectIBS = InputBuffer(InpIndex)
End If
End Select

End Function

Private Sub ClearInputBuffer()
Dim i As Integer

For i = 0 To UBound(InputBuffer)
InputBuffer(i) = ""
Next i

End Sub
